const RecomCourseModel = require('../db/models/recomCourse');
//import model


class recomCourseServer {
  async addRecomCourseData (data) {
    //检查cid是否存在 check cid is existed
    const cid = data.cid;
    const result = await RecomCourseModel.findOne({where: { cid }});
    //判断 check
    if (result) {
      //更新 update
      return await RecomCourseModel.update(data, {where: { cid }})
    }else {
      //创建 create
      return await RecomCourseModel.create(data);
    }
  }

  async getRecomCourseData () {
    return await RecomCourseModel.findAll({
      attributes: {
        exclude: ['mainTitle', 'posterUrl', 'description', 'teacherImg']
      }
    });
  }

  
  async changeRecomCourseStatus (id, status) {
    //连接公用中间件控制器
    const result =  await RecomCourseModel.update({ status }, {
      where: { cid: id }
    })
    return result[0];
  }



}

module.exports = new recomCourseServer();